package com.managertrade.dao.mysql.repository;

import com.managertrade.dto.PredictRecordDTO;
import com.managertrade.dto.SimplePredictRecordDTO;
import com.managertrade.dto.vo.BatchDTO;
import com.managertrade.dao.mysql.entity.PredictRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PredictRecordRepository extends JpaRepository<PredictRecord, Long> {
    List<PredictRecord> findByBatchName(String batchName);
    @Query("SELECT new com.managertrade.dto.vo.BatchDTO(p.batchId, p.batchName) " +
            "FROM PredictRecord p " +
            "WHERE p.userId = :userId " +
            "GROUP BY p.batchId, p.batchName " +
            "ORDER BY MAX(p.createdTime) DESC")
    List<BatchDTO> findDistinctBatchesByUserId(@Param("userId") Long userId);

    @Query("select new com.managertrade.dto.PredictRecordDTO(p.id, p.batchId, p.batchName, p.predictDirection, " +
            "p.actualDirection, p.isSuccess, FUNCTION('DATE_FORMAT', p.createdTime, '%Y-%m-%d %H:%i:%s')) " +
            "from PredictRecord p " +
            "where (:batchId is null or p.batchId = :batchId)")
    Page<PredictRecordDTO> findByBatchIdOrAll(@Param("batchId") String batchId, Pageable pageable);



    @Query("SELECT new com.managertrade.dto.SimplePredictRecordDTO(p.id, p.batchId, p.predictDirection, p.actualDirection, p.isSuccess) FROM PredictRecord p WHERE p.batchId = :batchId")
    List<SimplePredictRecordDTO> findLightweightByBatchId(@Param("batchId") String batchId);



    void deleteByBatchId(String batchId);
}